package com.kpmg.questionnaire.controller;

import com.kpmg.questionnaire.model.QuestiontableEntity;

import com.kpmg.questionnaire.model.UserEntity;
import com.kpmg.questionnaire.repository.QuestiontableRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

/**
 * Created by xionghao on 2016/11/9.
 */
@Controller
public class QuestionTableController {

    @Autowired
    private QuestiontableRepository questiontableRepository;

    //    问卷页面
    @RequestMapping(value = "/questiontable", method = RequestMethod.GET)
    public String getQuestiontables(ModelMap modelMap){
//        List<QuestionEntity> questionList = questionRepository.findAll();
        List<QuestiontableEntity> list = questiontableRepository.findAll();
        modelMap.addAttribute("list", list);
        return "questiontable/list";
    }

    // 添加问卷
    @RequestMapping(value = "/questiontable/addItem", method = RequestMethod.GET)
    public String addItem(){
        return "questiontable/addItem";
    }

    // 添加处理
    @RequestMapping(value = "/questiontable/addItemPost", method = RequestMethod.POST)
    public String addItemPost(@ModelAttribute("questiontable") QuestiontableEntity  questiontableEntity){
        // 向数据库添加一个用户
        //userRepository.save(userEntity);

        // 向数据库添加一个表单，并将内存中缓存区的数据刷新，立即写入数据库，之后才可以进行访问读取
        questiontableRepository.saveAndFlush(questiontableEntity);

        // 返回重定向页面
        return "redirect:/questiontable";
    }

    // 查看用户详细信息
    // @PathVariable可以收集url中的变量，需匹配的变量用{}括起来
    // 例如：访问 localhost:8080/showUser/1 ，将匹配 userId = 1
    @RequestMapping(value = "/questiontable/showItem/{itemId}", method = RequestMethod.GET)
    public String showItem(@PathVariable("itemId") Integer itemId, ModelMap modelMap ){
       // UserEntity userEntity = userRepository.findOne(userId);
        QuestiontableEntity item = questiontableRepository.findOne(itemId);
        modelMap.addAttribute("item", item);
        return "questiontable/itemDetail";
    }
    // 更新信息页面
    @RequestMapping(value = "/questiontable/updateItem/{itemId}", method = RequestMethod.GET)
    public String updateItem(@PathVariable("itemId") Integer itemId, ModelMap modelMap){
        QuestiontableEntity item = questiontableRepository.findOne(itemId);
        modelMap.addAttribute("item", item);
        return "questiontable/updateItem";
    }

    // 处理用户修改请求
    @RequestMapping(value = "/questiontable/updateItemPost", method = RequestMethod.POST)
    public String updateItemPost(@ModelAttribute("questiontable") QuestiontableEntity questiontableEntity){
        questiontableRepository.updateItem(
                questiontableEntity.getName(),
                questiontableEntity.getQuestion(),
                questiontableEntity.getNote(),
                questiontableEntity.getId()
        );
        return "redirect:/questiontable";
    }

    // 删除用户
    @RequestMapping(value = "/questiontable/deleteItem/{itemId}", method = RequestMethod.GET)
    public String deleteUser(@PathVariable("itemId") Integer itemId){
        // 删除id为userId的用户
        questiontableRepository.delete(itemId);
        // 立即刷新数据库
        questiontableRepository.flush();
        return "redirect:/questiontable";
    }


}
